#include "model.h"

Model::Model(QObject *parent, Bot* botController)
	: QAbstractTableModel(parent)
{
	_botController=botController;
	allData = new  vector<Entry*>(_botController->getVector());
	rows = allData->size();
}

int Model::rowCount(const QModelIndex & /*parent*/) const
{
    return rows;
}

//-----------------------------------------------------------------
int Model::columnCount(const QModelIndex & /*parent*/) const
{
    return COLS;
}


//Set Up Headers

QVariant Model::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (role == Qt::DisplayRole)
    {
        //Display row numbers
		if (orientation == Qt::Vertical){
			return section + 1;
		}
		
		if (orientation == Qt::Horizontal) {
            switch (section)
            {
            case 0:
                return QString("Description");
            case 1:
                return QString("Start");
            case 2:
                return QString("End");
			case 3:
				return QString("Status");
            }
        }

		

    }
    return QVariant();
}

//-----------------------------------------------------------------
//Read Data and put in Model
QVariant Model::data(const QModelIndex &index, int role) const
{
	int col = index.column();
	int row = index.row();
    if (role == Qt::DisplayRole)
    {
		Entry *entries = (*allData)[row];

	  switch(col){
			case 0:
				return QString::fromStdString(entries->getName());
				break;
			case 1:
				return QDateTime(entries->getStartDate(), entries->getStartTime());
				break;
			case 2:
				return QDateTime(entries->getEndDate(), entries->getEndTime());
				break;
			case 3:
				return QString::fromStdString(entries->getStatusString());
				break;
			}
    }
	
	if(role == Qt::BackgroundRole)
        {
			Entry *entries = (*allData)[row];
			QDateTime dateAndTime(entries->getEndDate(), entries->getEndTime());
            if(dateAndTime < QDateTime::currentDateTime() && !(entries->getStatus())){
               // background for overdue tasks is red!
				QBrush redBackground("#FF2D0D");
			   return redBackground;
            }
			else if(true/*entries->isStillOngoing()*/){
				QBrush greenBackground("#008000");
				return greenBackground;
			}
			
			// otherwise background is white
        
        }


	//For font color
	if(role == Qt::ForegroundRole)
        {
			Entry *entries = (*allData)[row];
			QDateTime dateAndTime(entries->getEndDate(), entries->getEndTime());
            if(dateAndTime < QDateTime::currentDateTime() && !(entries->getStatus())){
               // background for overdue tasks is red!
				QBrush whiteFont("#FFFFFF");
			   return whiteFont;
            }
			else if(true/*entries->isStillOngoing()*/){
				QBrush whiteFont("#FFFFFF");
				return whiteFont;
			}
			
			// otherwise background is white
        
        }


	 if(role == Qt::CheckStateRole && index.column() == 0)
	 {
         Entry *entries = (*allData)[row];
		 if(entries->getStatus() == false)
			 return Qt::Unchecked;
		 else
			 return Qt::Checked;
	 }

	 if (role == Qt::EditRole)
    {
		Entry *entries = (*allData)[row];

	  switch(col){
			case 0:
				return QString::fromStdString(entries->getName());
				break;
			case 1:
				return QDateTime(entries->getStartDate(), entries->getStartTime());
				break;
			case 2:
				return QDateTime(entries->getEndDate(), entries->getEndTime());
				break;
			case 3:
				return QString::fromStdString(entries->getStatusString());
				break;
			}
    }
	
    return QVariant();
}

//-----------------------------------------------------------------
//! [quoting mymodel_e]
bool Model::setData(const QModelIndex & index, const QVariant & value, int role)
{

    if (role == Qt::EditRole)
    {

		int col = index.column();
		int row = index.row();
		
		_botController->editFromGui(row, col, value);
		delete allData;
		allData = new  vector<Entry*>(_botController->getVector());

		/*switch(col){

			case 0:
				(*allData)[row]->setName(value.toString().toStdString());
				break;
			case 1:
				(*allData)[row]->setStartDate(value.toDate());
				(*allData)[row]->setStartTime(value.toTime());
				break;
			case 2:
				(*allData)[row]->setEndDate(value.toDate());
				(*allData)[row]->setEndTime(value.toTime());
				break;
			case 3:
				//For Bool Complete need to set a checkbox or smth
				break;
		}*/

        emit editCompleted();
    }

	 if (role == Qt::CheckStateRole)
    {
        if ((Qt::CheckState)value.toInt() == Qt::Checked)
        {
            //user has checked item
            return true;
        }
        else
        {
            //user has unchecked item
            return false;
        }
    }


    return true;
}

//! [quoting mymodel_e]

//-----------------------------------------------------------------
//! [quoting mymodel_f]
Qt::ItemFlags Model::flags(const QModelIndex & index) const
{
	//if (index.column()==0) 
		 // return Qt::ItemIsSelectable |  Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
    return Qt::ItemIsSelectable |  Qt::ItemIsEditable | Qt::ItemIsEnabled| Qt::ItemIsUserCheckable;
}

Model::~Model()
{

}
